/*
 * This file is part of LexiToolsMobile
 *  
 * Author: Jerzy Wawro, Jozef Kamycki
 * ver. 1.00
 * Copyright: Tenar (http://www.tenar.pl)
 *
 * Mobile LexiTools is an open source project; you can redistribute it 
 * and/or modify it under the terms of the license as published -  GPL with 
 * the following modification:  fragments of the code may be not copyleft 
 * (hidden), to protect contents of the dicionaries.
 * In this case the author of non-public fragments of the code is obliged 
 * to make available functionally equivalent code for unprotected dictionaries.
 * 
 * Mobile LexiTools is distributed WITHOUT ANY WARRANTY.
 * See the license for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with LexiToolsMobile (see: gpl.txt); if not - you can find it 
 * on web page http://www.fsf.org/, or write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package lt.main;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
//import java.io.IOException;

//import javax.microedition.io.Connector;
//#if FileConnection=="1"
//# import javax.microedition.io.file.FileConnection;
//#endif
import javax.microedition.rms.RecordStore;
// import javax.microedition.rms.RecordStoreException;

import lt.util.device;

public class params {

    public String sLANG0;
    public String sLANG1;
    public String sSELECT;
    public String sWASSEND;
    public String sCONNECTION_ERROR;
    public String sSEARCH;
    public String sSEARCHING;
    public String sSEARCH_EXAMPLES;
    public String sENTER_NUMBER;
    public String sMESSAGE;
    public String sBACK;
    public String sSEND;
    public String sCLEAR;
    public String sCLOSE;
    public String sSETTINGS;
    public String sSAVE;
    public String sCANCEL;
    public String sSMS_SENDING;
    public String sSMS_EDIT;
    public String sWRONG_SMS_ADDRESS;
    public String sSMS_WAS_SEND;
    public String sSMS_WAS_NOT_SEND;
    public String sNOT_FOUND;
    public String sNOT_FOUND_WEB;
    public String sENTER_TEXT;
    public String sWEB_SEARCH;
    public String sYES;
    public String sNO;
    public String sHASH_KEYBOARD;
    public String sABOUT_INFO;
    
    public String sSTORE; 
    public String sSTORED_TEXT; 
    public String sLANG0_PRON;
    public String sLANG1_PRON;
    
    public String sINTERFACE;
    public String sTRANS_DIR;
    public String sDIR01;
    public String sDIR10;
    public String sDUMMY_DICT;
    public String sOPT_ONLY_FIRST;
    public String sOPT_SEARCH_WEB;
    public String sOPT_GET_PRON;
    public String sFIND_DICT;
    public String sNUMBER_OF_DICT;
    public String sNOTE;
    
    public String sSELECT_ACTION;
    public String sSELECT_STORE_SMS;
    public String sSELECT_APPEND_SMS;
    public String sSELECT_STORE_BACK;
    public String sSELECT_APPEND_BACK;
                    
    public String sAPP_NAME = "LexiTools 1.10-b26";
    //?public String sRS_MAIN = "main";
    public String sURL = "http://www.lexitools.org/lt3.php"; 
    public String sSAY_URL = "http://www.lexitools.org/cgi-bin/snd.pl?tekst=";
    public String sPL_EN_IMG = "/lt/images/pl_en.gif";
    public String sWAITING_IMG = "/lt/images/waiting.png";
    public String filePath = "file:///c:/other/lt.ini";
    
    public boolean paramsInRS = true; 
    public int intLanguage = 0; // 0 = polski 1=angielski
    public int dictDirection = 0; // Kierunek t�umaczenia 0 = polski -> angielski
    
    public int iPreviewMax = 10; // Ilo�� hase� podpowiedzi

    public int iEquivMax = 25; // Ilo�� hase� wyboru
    
    public int dictDummy = 0; // S�ownik domy�lny (0 = minimum; 1 = M.Tasiemskiego etc)
    public boolean dictOnlyDummy = false; //Tylko ze s�ownika domy�lnego
    
    public int iGET_LIST = 0;
    public int iGET_PRON = 0;
    
    public int iSOFT_BUTTONS = 2; // ilosc przyciskow
    public int iSPECIAL_KEY_BACK = 0; 
    public int iSPECIAL_KEY_MENU = 0; 
    public int iSPECIAL_KEY_HOME = 0; 
    public int iBROWSER_SEPARATE_LANG = 0;
    public int iPLATFORM = 0;
    public int iS60 = 0;
    
    public String sBlank = " ";

    public boolean dictInWeb = false; //Szuka� w internecie
    public boolean dictGetPronunciation; // Pobiera� wymow� z internetu
    
    public device dev = new device();
    
    public void switchLang() {
        if (intLanguage==0) {
            LocalizationSupport.initLocalizationSupport("pl_PL"); // polski
        }
        else {
            LocalizationSupport.initLocalizationSupport("en_US"); // English
        }
//        if (dictDirection==0) {
            sLANG0 = LocalizationSupport.getMessage("LANG0");
            sLANG1 = LocalizationSupport.getMessage("LANG1");
/*        } else {
            sLANG0 = LocalizationSupport.getMessage("LANG1");
            sLANG1 = LocalizationSupport.getMessage("LANG0");
        }
 */
            sSELECT = LocalizationSupport.getMessage("SELECT");
            sWASSEND = LocalizationSupport.getMessage("WASSEND");
            sCONNECTION_ERROR = LocalizationSupport.getMessage("CONNECTIONERROR");
            sSEARCH = LocalizationSupport.getMessage("SEARCH");
            sSEARCHING = LocalizationSupport.getMessage("SEARCHING");
            sSEARCH_EXAMPLES = LocalizationSupport.getMessage("SEARCHEXAMPLES");
            sENTER_NUMBER = LocalizationSupport.getMessage("ENTERNUMBER");
            sMESSAGE = LocalizationSupport.getMessage("MESSAGE");
            sBACK = LocalizationSupport.getMessage("BACK");
            sSEND = LocalizationSupport.getMessage("SEND");
            sCLEAR = LocalizationSupport.getMessage("CLEAR");
            sCLOSE = LocalizationSupport.getMessage("CLOSE");
            sSETTINGS = LocalizationSupport.getMessage("SETTINGS");
            sCANCEL = LocalizationSupport.getMessage("CANCEL");
            sSMS_EDIT = LocalizationSupport.getMessage("SMSEDIT");
            sSMS_SENDING = LocalizationSupport.getMessage("SMSSENDING");
            sWRONG_SMS_ADDRESS = LocalizationSupport.getMessage("WRONGSMSADDRESS");
            sSMS_WAS_SEND = LocalizationSupport.getMessage("SMSWASSEND");
            sSMS_WAS_NOT_SEND = LocalizationSupport.getMessage("SMSWASNOTSEND");
            sNOT_FOUND = LocalizationSupport.getMessage("NOTFOUND");
            sNOT_FOUND_WEB = LocalizationSupport.getMessage("NOTFOUNDWEB");
            sENTER_TEXT = LocalizationSupport.getMessage("ENTERTEXT");
            sWEB_SEARCH = LocalizationSupport.getMessage("WEBSEARCH");
            sSAVE = LocalizationSupport.getMessage("SAVE");
            sYES = LocalizationSupport.getMessage("YES");
            sNO = LocalizationSupport.getMessage("NO");
            sHASH_KEYBOARD = LocalizationSupport.getMessage("HASHKEYBOARD");
            sABOUT_INFO = LocalizationSupport.getMessage("ABOUTINFO");

            sLANG0_PRON = LocalizationSupport.getMessage("LANG0PRON");
            sLANG1_PRON = LocalizationSupport.getMessage("LANG1PRON");
    
            sSTORE = LocalizationSupport.getMessage("STORE");
            sSTORED_TEXT = LocalizationSupport.getMessage("STOREDTEXT");
    
            sINTERFACE = LocalizationSupport.getMessage("INTERFACE");
            sTRANS_DIR = LocalizationSupport.getMessage("TRANSDIR");
            sDIR01 = LocalizationSupport.getMessage("DIR01");
            sDIR10 = LocalizationSupport.getMessage("DIR10");
            sDUMMY_DICT = LocalizationSupport.getMessage("DUMMYDICT");
            sOPT_ONLY_FIRST = LocalizationSupport.getMessage("OPTONLYFIRST");
            sOPT_SEARCH_WEB = LocalizationSupport.getMessage("OPTSEARCHWEB");
            sOPT_GET_PRON = LocalizationSupport.getMessage("OPTGETPRON");
            sOPT_ONLY_FIRST = LocalizationSupport.getMessage("OPTONLYFIRST");
            sOPT_SEARCH_WEB = LocalizationSupport.getMessage("OPTSEARCHWEB");
            sOPT_GET_PRON = LocalizationSupport.getMessage("OPTGETPRON");
            sFIND_DICT = LocalizationSupport.getMessage("FINDDICT");
            sNUMBER_OF_DICT = LocalizationSupport.getMessage("NUMBEROFDICT");
            sNOTE = LocalizationSupport.getMessage("NOTE");

            sSELECT_ACTION = LocalizationSupport.getMessage("SELECTACTION");
            sSELECT_STORE_SMS = LocalizationSupport.getMessage("SELECTSTORESMS");
            sSELECT_APPEND_SMS = LocalizationSupport.getMessage("SELECTAPPENDSMS");
            sSELECT_STORE_BACK = LocalizationSupport.getMessage("SELECTSTOREBACK");
            sSELECT_APPEND_BACK = LocalizationSupport.getMessage("SELECTAPPENDBACK");
    }
    
    public params() {
        if (paramsInRS) ReadSettingsRS();
        else ReadSettingsPIM();
        switchLang();
        iPLATFORM=device.platform();
        if (iPLATFORM==device.iPLATFORM_NOKIA) {
            sBlank = "_";
            iSOFT_BUTTONS = 3;
//#if S60=="1"
//#             sBlank="";
//#             iSOFT_BUTTONS = 2;
//#endif        
        } else  if (iPLATFORM==device.iPLATFORM_SONY_ERICSSON) {
            iBROWSER_SEPARATE_LANG=1;
        } else  if (iPLATFORM==device.iPLATFORM_SAMSUNG) {
            iBROWSER_SEPARATE_LANG=1;
        }
   }


    public void ReadSettingsRS() {
        try {
            // otwarcie RS
            RecordStore rs = RecordStore.openRecordStore("LT", true);
            if (rs.getNumRecords() > 0) { // record store istnieje
//                System.out.println("Odczytano Ittniejący Record Set  " + rs.getNumRecords());
                // odczytanie pierwszego rekordu
                byte[] data = rs.getRecord(1);
                // przekształcenie do striga
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                DataInputStream dis = new DataInputStream(bais);
                intLanguage = dis.readInt();
                dictDirection = dis.readInt();
                dictInWeb = dis.readBoolean();
                dictGetPronunciation = dis.readBoolean();
                dictOnlyDummy = dis.readBoolean();
                dictDummy = dis.readInt();
                iPreviewMax = dis.readInt();
                iEquivMax = dis.readInt();

                rs.closeRecordStore();

            }
        } catch (Exception e) {
//!!!         getDisplay().setCurrent(getAlert(), getFormMain());     
        }

    }
    

    public void WriteSettingsRS() {
        int i,n;
        
        try {
            // otwarcie RS
            RecordStore rs = RecordStore.openRecordStore("LT", true);
            byte[] data;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
// zapis zmiennych do record Store
//            dos.writeBoolean(polZnak);
            dos.writeInt(intLanguage);
            dos.writeInt(dictDirection);
            dos.writeBoolean(dictInWeb);
            dos.writeBoolean(dictGetPronunciation);
//                            dos.writeBoolean(sugerujZTlum);
            dos.writeBoolean(dictOnlyDummy);
            dos.writeInt(dictDummy);
            dos.writeInt(iPreviewMax);
            dos.writeInt(iEquivMax);
            dos.flush();
// przekształć na binarną tablicę znaków

            data = baos.toByteArray();
// zapisanie record seta
            n=rs.getNumRecords();
            if (n>1) for (i=1; i<n; i++) rs.deleteRecord(i); // posprzątanie - po błędzie - usunąć
            if (rs.getNumRecords() > 0) { // record store istnieje
                rs.setRecord(0, data, 0, data.length);
            } else {
                rs.addRecord(data, 0, data.length);
//                    System.out.println("Zapisano początkowy Recird set" + rs.getNumRecords());
            }
            rs.closeRecordStore();
            dos.close();
            baos.close();
        } catch (Exception e) {
//!!!         getDisplay().setCurrent(getAlert(), getFormMain());     
        }

    }
    
    public void ReadSettingsPIM() {
//#if FileConnection=="1"        
//#         try {
//#             FileConnection fileConnect = (FileConnection) Connector.open(filePath+"lt.ini",Connector.READ_WRITE);
//#             if (!fileConnect.exists()) {
//#                 System.out.println("Brak pliku ustawień");
//# /*
//#  DataOutputStream outputStream = (DataOutputStream) fileConnect.openDataOutputStream();
//#                 //                      outputStream.writeBoolean(Opcje.isSelected(0));
//# */
//#             } else {
//#                 System.out.println("Jest plik ustawień");
//#                 DataInputStream inputStream;
//#                 try {
//#                     inputStream = (DataInputStream) fileConnect.openDataInputStream();
//#                     dictInWeb = inputStream.readBoolean();
//#                     dictGetPronunciation = inputStream.readBoolean();
//#                     dictOnlyDummy = inputStream.readBoolean();
//#                     dictDirection = inputStream.readInt();
//#                     intLanguage = inputStream.readInt();
//#                     dictDummy = inputStream.readInt();
//#                     iPreviewMax = inputStream.readInt();
//#                     iEquivMax = inputStream.readInt();
//# 
//#                     inputStream.close();
//# 
//#                 } catch (IOException ex) {
//#                     ex.printStackTrace();
//# //                    getDisplay().setCurrent(getAlert(), getFormMain());
//#                 } finally {
//#                     try {
//#                         fileConnect.close();
//#                     } catch (IOException ex) {
//#                         ex.printStackTrace();
//# //                        getDisplay().setCurrent(getAlert(), getFormMain());
//#                     }
//#                 }
//#             }
//#         } catch (IOException ex) {
//#             ex.printStackTrace();
//#   //          getDisplay().setCurrent(getAlert(), getFormMain());
//#         }
//#endif        
    }

 
    public void WriteSettingsPIM() {
//#if FileConnection=="1"        
//#         try {
//#             FileConnection fileConnect = (FileConnection) Connector.open(filePath+"lt.ini",Connector.READ_WRITE);
//#             try {
//#                  DataOutputStream outputStream = (DataOutputStream) fileConnect.openDataOutputStream();
//#                  outputStream.writeBoolean(dictInWeb);
//#                  outputStream.writeBoolean(dictGetPronunciation);
//#                  outputStream.writeBoolean(dictOnlyDummy);
//#                  outputStream.writeInt(dictDirection);
//#                  outputStream.writeInt(intLanguage);
//#                  outputStream.writeInt(dictDummy);
//#                  outputStream.writeInt(iPreviewMax);
//#                  outputStream.writeInt(iEquivMax);
//#                  outputStream.close();
//#                 } catch (IOException ex) {
//#                     ex.printStackTrace();
//# //                    getDisplay().setCurrent(getAlert(), getFormMain());
//#                 } finally {
//#                     try {
//#                         fileConnect.close();
//#                     } catch (IOException ex) {
//#                         ex.printStackTrace();
//# //                        getDisplay().setCurrent(getAlert(), getFormMain());
//#                     }
//#                 }
//#         } catch (IOException ex) {
//#             ex.printStackTrace();
//#   //          getDisplay().setCurrent(getAlert(), getFormMain());
//#         }
//#endif
    }

    
}